package com.hdy.web.controller;

import com.hdy.web.dao.SkuInfoDao;
import com.hdy.web.pojo.SkuInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/Goods")
public class GoodsController {
    @Resource
    SkuInfoDao skuInfoDao;
    @ResponseBody
    @GetMapping("/lookup")
    public List<SkuInfo> lookup(){
        try {
            return skuInfoDao.getAll();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/insert")
    public String insert(@RequestParam("commodityNo")  String no,
                         @RequestParam(value = "costPrice",required = false) Double costPrice,
                            @RequestParam(value = "sellingPrice",required = false)  Double sellingPrice,
                        @RequestParam("remark")  String remark){
        if (no.equals("")||costPrice==null||sellingPrice==null){
            return "404";
        }
        int insert = skuInfoDao.insert(new SkuInfo(null, no, costPrice, sellingPrice, remark));
        if (insert!=1){
            return "404";
        }
        return "lookupGoods";
    }
    @PostMapping("/delete")
    public String delete(@RequestParam("id") String id,@RequestParam("commodityID") String commodityID){
        if (id!=null&&!"".equals(id)){
            skuInfoDao.deleteById(id);
            return "lookupGoods";
        }
        if (commodityID!=null &&!"".equals(commodityID)){
            skuInfoDao.deleteByCommodity(commodityID);
            return "lookupGoods";
        }
    return "404";
    }
    @PostMapping("/update")
    public String update(@RequestParam(value = "ID",required = false) Integer ID,
                            @RequestParam(value = "CommodityID",required = false)  String no,
                         @RequestParam(value = "CostPrice",required = false) Double costPrice,
                         @RequestParam(value = "SellingPrice",required = false)  Double sellingPrice,
                         @RequestParam(value = "Remark",required = false)  String remark){
        if (ID == null){
            return "404";
        }else {
            int i = skuInfoDao.update(ID, no, costPrice,sellingPrice , remark);
            return i==1?"lookupGoods":"404";
        }
    }
}
